/*
 * Main.c
 *
 *  Created on: 16 juli 2021
 *      Author: Daniel Mårtensson
 */

#include <stdio.h>

/* Include Open SAE J1939 */
#include "Open_SAE_J1939/Open_SAE_J1939.h"

/* Include ISO 11783 */
#include "ISO_11783/ISO_11783-7_Application_Layer/Application_Layer.h"

int main() {

	/* Create our J1939 structure with two ECU */
	J1939 j1939_1 = {0};
	J1939 j1939_2 = {0};

	/* Important to sent all non-address to 0xFF - Else we cannot use ECU address 0x0 */
	uint8_t i;
	for(i = 0; i < 255; i++){
		j1939_1.other_ECU_address[i] = 0xFF;
		j1939_2.other_ECU_address[i] = 0xFF;
	}

	/* Set the ECU address */
	j1939_1.information_this_ECU.this_ECU_address = 0x80;												/* From 0 to 253 because 254 = error address and 255 = broadcast address */
	j1939_2.information_this_ECU.this_ECU_address = 0xFD;

	/* Send a command from ECU 1 to ECU 2 */
	ISO_11783_Send_General_Purpose_Valve_Command(&j1939_1, 0xFD, 100, FAIL_SAFE_MODE_ACTIVATED, VALVE_STATE_INITIALISATION, 60000);

	/* Read the command for ECU 2 */
	Open_SAE_J1939_Listen_For_Messages(&j1939_2);

	/* Display what we got */
	printf("Standard flow = %i\nValve state = %i\nFail safe mode = %i\nExtended flow = %i\nFrom ECU address = 0x%X"
			,j1939_2.from_other_ecu_general_purpose_valve_command.standard_flow
			,j1939_2.from_other_ecu_general_purpose_valve_command.valve_state
			,j1939_2.from_other_ecu_general_purpose_valve_command.fail_safe_mode
			,j1939_2.from_other_ecu_general_purpose_valve_command.extended_flow
			,j1939_2.from_other_ecu_general_purpose_valve_command.from_ecu_address);

	return 0;
}
